<?php
/**
 * Authorizer Repo
 *
 * Author: Zeon
 * Date: 2017/7/21
 * Time: 16:24
 * Created by PhpStorm.
 */
namespace App\Services\OpenWechat;

use App\Models\OpenWechat\Authorizer;
use App\Services\Utils\ApiChecker;

class AuthorizerRepository
{
    use ApiChecker;

    /**
     * @var Authorizer
     */
    private $authorizer;

    public function __construct(Authorizer $authorizer)
    {
        $this->authorizer = $authorizer;
    }

    /**
     * @param string $authorizer_appid
     *
     * @return Authorizer
     */
    public function firstOrCreateAuthorizationInfo($authorizer_appid): Authorizer
    {
        /** @var Authorizer $author */
        $author = $this->authorizer->firstOrCreate([
            'authorizer_appid' => $authorizer_appid,
        ]);

        return $author;
    }

    /**
     * 检查公众号类型必须为服务号
     */
    public function checkIsTypeService(): void
    {
        $this->checkIsTrue(
            $this->authorizer->isTypeService(),
            trans('app.open_wechat.authorizer_is_not_type_service')
        );
    }

}